package com.sentiwt.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sentiwt.jpa.NoiseWord;
 
@Component
public class NoiseWordDAO {
    // Injected database connection:
    @PersistenceContext private EntityManager em;
 
    // Stores a new word:
    @Transactional
    public void save(NoiseWord noiseWord) {
        em.persist(noiseWord);
    }
 
    // Retrieves all the words:
    public List<NoiseWord> getAllNoiseWords() {
        TypedQuery<NoiseWord> query = em.createQuery(
            "SELECT g FROM NoiseWord g ORDER BY g.id", NoiseWord.class);
        return query.getResultList();
    }
    
    public boolean isNoiseWord(String name) {
    	Query q = em.createQuery("SELECT r FROM "+NoiseWord.class.getName()+" r WHERE r.name = :name");
    	q.setParameter("name", name);
    	if(q.getSingleResult()!=null) {
    		return true;
    	}
    	return false;
    }
    
}
